#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qDebug()<<"available drivers: ";
    connection();
    pModel = new QSqlTableModel(this,sqldb);

    initial_tablemode();

    ui->tableView->setModel(pModel);

    initial_tableview();

    connect(ui->btn_updata,&QPushButton::clicked,this,&MainWindow::update);

    connect(ui->btn_insert,&QPushButton::clicked,this,&MainWindow::insert);

    connect(ui->btn_delete,&QPushButton::clicked,this,&MainWindow::selection);

    connect(ui->btn_quit,&QPushButton::clicked,this,&MainWindow::close);

}

void MainWindow::connection()
{
    sqldb = QSqlDatabase::addDatabase("QMYSQL");
    sqldb.setHostName("localhost");
    sqldb.setDatabaseName("team");
    sqldb.setUserName("root");
    sqldb.setPassword("123456");
    if(!sqldb.open())
    {
        QMessageBox::critical(this,"error",sqldb.lastError().text(),QMessageBox::Cancel);
    }
}

void MainWindow::initial_tablemode()
{
    //选择表
    pModel->setTable("roster");
    //设置表头
    pModel->setHeaderData(0,Qt::Horizontal,QStringLiteral("编号"));
    pModel->setHeaderData(1,Qt::Horizontal,QStringLiteral("姓名"));
    pModel->setHeaderData(2,Qt::Horizontal,QStringLiteral("性别"));
    pModel->setHeaderData(3,Qt::Horizontal,QStringLiteral("年龄"));
    //选择模型 setEditStrategy
    //QSqlTableModel::OnFieldChage
    //QSqlTableModel::OnRowChage
    //QSqlTableModel::OnManualSubmit
    pModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    pModel->select();

}

void MainWindow::initial_tableview()
{
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableView->setSelectionBehavior(QAbstractItemView::QAbstractItemView::SelectRows);
    ui->tableView->verticalHeader()->hide();
    ui->tableView->horizontalHeader()->setStyleSheet("QHeaderView::section{background-color:rgb(225,225,225)};");
    connect(ui->tableView->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(sortByColumn(int)));
}


void MainWindow::selection()
{
    if(!ui->lineEdit->text().isEmpty())
    {
        pModel->setFilter("num='"+ui->lineEdit->text()+"'");
    }
    else
    {
        pModel->setFilter("");
    }
    pModel->select();
}

void MainWindow::insert()
{
    Dialog dialog(this);
    dialog.exec();
    QSqlRecord record = pModel->record();
    record.setValue(0,dialog.num);
    record.setValue(1,dialog.name);
    record.setValue(2,dialog.sex);
    record.setValue(3,dialog.age);
    pModel->insertRecord(pModel->rowCount(),record);
    if(!pModel->submit())
    {
        QMessageBox::critical(this,"error",QStringLiteral("添加失败"),QMessageBox::Cancel);
    }
    else
    {
        QMessageBox::information(this,"succse", QStringLiteral("添加成功"),QMessageBox::Ok);
    }
}

void MainWindow::update()
{
    pModel->database().transaction();
    if(pModel->submitAll())
        pModel->database().commit();
    else{
        pModel->database().rollback();
        QMessageBox::warning(this,tr("tableModel"),tr("error: %1").arg(pModel->lastError().text()));
    }
    pModel->revertAll();
}

void MainWindow::mydelete()
{
    pModel->removeRow(ui->tableView->currentIndex().row());
    pModel->submit();
}

MainWindow::~MainWindow()
{
    sqldb.close();
    delete ui;
}
